package com.pearl.jacksoncore.demo.feature;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.pearl.jacksoncore.demo.response.R;

import java.util.Date;

/**
 * @author TD
 * @version 1.0
 * @date 2024/4/2
 */
public class MapperFeatureTest {

    public static void main(String[] args) throws JsonProcessingException {
        JsonMapper jsonMapper = JsonMapper.builder()
                //.disable(MapperFeature.USE_ANNOTATIONS)
                //.disable(MapperFeature.INFER_PROPERTY_MUTATORS)
                //.enable(MapperFeature.ALLOW_VOID_VALUED_PROPERTIES)
                //.disable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
                .build();
        // USE_ANNOTATIONS
        User user = new User();
        user.setId(1767798780627279333L);
        user.setName("测试");
        user.setBirthday(new Date());
        String jsonStrAA = jsonMapper.writeValueAsString(user);
        System.out.println(jsonStrAA);

        // AUTO_DETECT_CREATORS
        String json = "{\"name\": \"John\"}";
        PersonVO person = jsonMapper.readValue(json, PersonVO.class);
        System.out.println(person);

        // ALLOW_VOID_VALUED_PROPERTIES
        VoidVO voidVO=new VoidVO();
        String jsonByVoidVO = jsonMapper.writeValueAsString(voidVO);// 将对象序列化为 JSON
        System.out.println(jsonByVoidVO);

        // INFER_PROPERTY_MUTATORS ALLOW_FINAL_FIELDS_AS_MUTATORS
        String strByMutatorsVO="{\"name\":\"张三\",\"age\":15}";
        FinalPerson mutatorsVOByStr = jsonMapper.readValue(strByMutatorsVO, FinalPerson.class);
        System.out.println(mutatorsVOByStr);

        //
    }
}

